
//var loginer = -1;//用户id
//var loginerqian=0;//用户钱
var playxinxi={guanka:'',map:'',guankaid:'',guankaqian:''}//关卡信息
function ZhuceScene(){
            this.$me = $("<div id='formbg'></div>");
            this.$formbg2 = $("<div id='formbg2'></div>");
            this.$form=$("<form id='testFormzc' action='' method='post'></form>")
            this.$table=$("<table style='margin-left:50px;padding-top:18px'></table>")
            this.$loginName = $("<tr><td style='text-align:right;'>用户名：*</td><td><input type='text' id='loginName' name='loginName' class='required' placeholder='请输入6-20位数字、英文或中文' onblur=Regcheck()></input></td></tr>")
            this.$loginPsw = $("<tr><td style='text-align:right;'>密码：*</td><td><input type='text' id='loginPsw' name='password1' class='required' placeholder='请输入6-20位数字、英文'></input></td></tr>");
            this.$loginRePsw = $("<tr><td style='text-align:right;'>确认密码：*</td><td><input type='text' id='loginPsw2'  name='password2' class='required' placeholder='请重复密码'></input></td></tr>")
            this.$tishi = $("<tr><td style='text-align:right;' id='tishi'><td></td></tr>")
            this.$loginok = $("<a href='#' id='loginok'  style='margin-left: 100px;' class='button'></a>")
            this.$table.append(this.$loginName);
            this.$table.append(this.$loginPsw);
            this.$table.append(this.$loginRePsw);
            this.$table.append(this.$tishi);
            this.$form.append(this.$table);
            this.$form.append(this.$loginok);
            this.$formbg2.append(this.$form);
            this.$me.append(this.$formbg2);
            

          //退出按钮
            this.$but=$("<a href='#' id='retur'  class='retur' ></a>");
            this.$me.append(this.$but);
            this.$but.click(function(){
                director.runScene(new LoginScene());
            });

//******************点击注册************************         
  this.$loginok.click(function(){
  var name=document.getElementById("loginName").value;
  var repassword=document.getElementById("loginPsw2").value;
  var password=document.getElementById("loginPsw").value;
  
  if(name.length==0){
    document.getElementById("tishi").innerHTML="用户名不能为空";
    return ;
  }
  
  if(password.length==0){
    document.getElementById("tishi").innerHTML="密码不能为空";
    return ;
  }
  if(repassword!=password){
    document.getElementById("tishi").innerHTML="两次密码不同";
    return ;
  }
 
  if (document.getElementById("tishi").innerHTML=="用户名已存在") {
    return;}
  //组织数据为JSON格式发出
  var jsonobj1={"handleid":3,"name":name,"password":password};
  
  //用AJAX发送
  $.ajax({
    type:"post",
    url:"handle/handout.php",
    dataType:"json",
   data:jsonobj1,
    success:function(data){
      //错误的情况
      if(data['rs']=='error'){
        alert(data['inf']);
        return ;
      }     
      //正确的情况
      if(data['rs']=='success'){
        console.log(data['inf']);
        //alert(data['ruid']);
         director.runScene(new LoginScene());

        //localStorage.setItem("ruid",data['ruid']);
             ubike=new Ubike(data['ruid']);
             urider=new Urider(data['ruid']);
             uequip=new Uequip(data['ruid']);
             umission=new Umission(data['ruid']);
      }
    },
    error:function(responexml,name){
      alert("zhuce"+name);
     }
     });
 
         return false;
            });
        }
//******************注册信息验证************************
function Regcheck(){
   //组织数据为JSON格式发出
  var name=document.getElementById("loginName").value;
 // alert(name)
  var jsonobj={"handleid":2,"name":name};
  
  //用AJAX发送
  $.ajax({
    type:"post",
    url:"handle/handout.php",
   dataType:"json",
   data:jsonobj,
   //async:false,
    success:function(data){
      //错误的情况
      if(data['rs']=='error'){
       
       alert(data['errorinf']);
       director.runScene(new LoginScene());
      }     
      //正确的情况
      if(data['rs']=='success'){

        document.getElementById("tishi").innerHTML=data['successinf'];
      }

    },
    error:function(responexml,name){
      alert("zhuyan"+name);
     }
     });

}
//******************登陆信息验证************************
  function login(){
  var name=document.getElementById("denglname").value;
  var password=document.getElementById("denglpsw").value;
  if(name.length==0){
    document.getElementById("logintishi").innerHTML="用户名不能为空";
    return ;
  }
  if(password.length==0){
    document.getElementById("logintishi").innerHTML="密码不能为空";
    return ;
  }
  var jsonobj={"handleid":1,"name":name,"pwd":password};  
  //用AJAX发送
  $.ajax({
    type:"post",
    url:"handle/handout.php",
    dataType:"json",
    data:jsonobj,
    success:function(data){

    // alert(data);
      var rs_arr=[];
      $(data).each(function(i,val){
        console.log(data);
        var rs=[];
        for(var prop in val){
          rs.push(val[prop]);
          console.log(val[prop]);
        }
        //console.log(rs+"111");
        rs_arr.push(rs);
        console.log(rs_arr);
      });
      //错误的情况
      if(rs_arr[0][0]=='error'){
       
       alert(data['errorinf']);

      }     
      //正确的情况
      if(rs_arr[0][0]=='success'){
        director.runScene(new LoadingScene());
        //将用户的基本信息存储起来
        localStorage.setItem("luid",rs_arr[0][2]);
        localStorage.setItem("vip",rs_arr[0][3]);
        localStorage.setItem("ulevel",rs_arr[0][4]);
        localStorage.setItem("ucoin",rs_arr[0][5]);
        localStorage.setItem("uname",rs_arr[0][6]);
        //console.log(uid);
        luid=localStorage.getItem("luid");
        ulevel=localStorage.getItem("ulevel");
        ucoin=localStorage.getItem("ucoin");
        vip=localStorage.getItem("vip");
        uname=localStorage.getItem("uname");
        }
       },    
     error:function(responexml,name){
       alert("login"+name);
        }
     });
      return false;
 }
function LoginScene(){
            this.$me = $("<div class='loginscene'><div id='abc'></div></div>");
            this.$me.append("<input id='denglname' class='loginkuang'  type='text' name='name'></input>");
            this.$me.append("<input id='denglpsw' class='loginkuang'  type='password' name='pwd'></input>");
            this.$but1 = $("<button id='dengl' class='loginanniu' ></button>");
            this.$but2 = $("<button id='zhuce' class='zhuceanniu' ></button>");
            this.$tishi=$("<span style='text-align:right;' id='logintishi'></span>")
            this.$me.append(this.$but1);
            this.$me.append(this.$but2);
            this.$me.append(this.$tishi);
            var self=this;
           //修改密码 
            var $forgetpwd=$("<a href='#' class='forgetpwd'>修改密码？</a>");
            this.$me.append($forgetpwd);
            $forgetpwd.click(function(){
            	var $forget=$("<button class='wan' style='left:-20%;'></button>");
                self.$me.append($forget);
                var $forgetq=$("<div class='wanq' style='left: 90%;'></div>");
                $forget.append($forgetq);
                var $forget4=$("<span style='font-size: 20px' >用户名:</span>");
                $forget.append($forget4);
                var $forget1=$("<input id='uname' type='text'/><br>");
                $forget.append($forget1);
                var $forget5=$("<span  style='font-size: 20px;top:1px'>原密码:</span>")
                $forget.append($forget5);
                var $forget2=$("<input id='upsw' type='text'/><br>")
                $forget.append($forget2);
                var $forget6=$("<span  style='font-size: 20px;top:1px'>新密码:</span>")
                $forget.append($forget6);
                var $forget3=$("<input id='urepsw' type='text'/><br>")
                $forget.append($forget3);
                var $but=$("<button id='chobut'  style='font-size: 20px;background-color:#033F4C;top: 273px;left: 400px;'>确定</button>");
                $forget.append($but);
                $but.click(function(){
                	console.log($forget1.val(),$forget2.val(),$forget3.val())
                	return forgetpwd($forget1.val(),$forget2.val(),$forget3.val());         
                });
                $forget.fadeIn(300 ,function(){
                	$forget.animate({top:"-150%"});
                	//$forgetq.animate({top:"-20%"});
                     });
                    //关闭
                $forgetq.click(function(){
                	$forget.fadeOut(100);            
                });
            });
             //登录按钮
            this.$but1.click(function(){
               // console.log("登录")
                //验证用户名密码有效性
                return login();

            });
            //注册按钮
            this.$but2.click(function(){
                director.runScene(new ZhuceScene());
                $("#formbg").fadeIn(300 ,function(){
                    $("#formbg2").animate({top:"50%"})
                });
                        //验证用户名密码有效性
            });
        }

function forgetpwd(uname,pwd,repwd){
	var jsonobj3={"handleid":23,"uname":uname,"pwd":pwd,"repwd":repwd}; 
    $.ajax({
     type:"post",
     url:"handle/handout.php",
     dataType:"json",
     data:jsonobj3,
     success:function(data){
       //错误的情况
       if(data['rs']=='error'){
     	console.log("forget"+data['rs']);
        alert("用户名或密码错误");
       }     
       //正确的情况
       if(data['rs']=='success'){  
         console.log("forget"+data['rs']);
         director.runScene(new LoginScene());
        }  
       },
     error:function(responexml,name){
       console.log("forget"+name);
     }
   }); 
}
